/* 
 * Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *      * The names of its contributors may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package kameleon.exception;

import java.io.PrintWriter;
import java.io.StringWriter;

import kameleon.plugin.PlugInManager;

/**
 * Root class for all the personalized exceptions.
 * 
 * @author		Schnell Michaël
 * @version		1.0
 */
public class KameleonException extends Exception {

	/**
	 * Needed to serialize this class.
	 * 
	 * @see		java.io.Serializable
	 */
	private static final long serialVersionUID = 1529685710199024293L ;

	/**
	 * Builds a {@code KameleonException} with no error message.
	 */
	public KameleonException() {
		super() ;
	}// KameleonException()

	/**
	 * Builds a {@code KameleonException} with the given error message.
	 * 
	 * @param	message
	 * 			detailed error message for this exception
	 */
	public KameleonException(String message) {
		super(message) ;
	}// KameleonException(String)

	/**
	 * Builds a {@code KameleonException} with the given error message
	 * and cause.
	 * 
	 * @param	message
	 * 			detailed error message for this exception
	 * 
	 * @param	cause
	 * 			cause of this instance
	 */
	public KameleonException(String message, Throwable cause) {
		super(message, cause) ;
	}// KameleonException(String, Throwable)
	
	/**
	 * Given a {@code Throwable}, gets the full stack trace for the
	 * {@code Exception} or {@code Error} as a {@code String}. Returns 
	 * an empty string if something went wrong (so the caller won't fail with a
	 * {@code NullPointerException} later).
	 * 
	 * <p>Based on a version from kolichko Mark Kolich. 
	 * (http://mark.koli.ch/2009/05/howto-get-a-java-stack-track-as-a-string-getstacktraceasstring.html)
	 * 
	 * @param 	t
	 * 			exception whose stack trace is requested
	 * 
	 * @return	Full stack trace for the {@code Throwable} as a {@code String}
	 */
	public static String getStackTraceAsString(Throwable t) {
		final StringWriter sw = new StringWriter() ;
		final PrintWriter pw = new PrintWriter(sw, true) ;
		StringBuilder trace = new StringBuilder() ;

		try {
			t.printStackTrace(pw) ;
			pw.flush() ;
			sw.flush() ;
			trace.append(sw.toString()) ;
		} catch (Exception e) { 
			/* Catch the exception to prevent the caller from failing */
		} finally {
			try {
				sw.close() ;
				pw.close() ;
			} catch (Exception e) { 
				/* Catch the exception to prevent the caller from failing */
			}// try
		}// try
		trace.append(PlugInManager.LINE_FEED) ;
		String rawTrace = trace.toString() ;
		String formattedTrace = rawTrace.replaceAll(
				PlugInManager.TAB, PlugInManager.TAB_REPLACEMENT) ;

		return formattedTrace ;
	}// getStackTraceAsString(Throwable)

}// class KameleonException